package cn.school.demo.ui.answer

import android.graphics.Color
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import cn.school.demo.MyApplication
import cn.school.demo.R
import cn.school.demo.databinding.FragmentAnswerScoreBinding
import com.mylhyl.circledialog.CircleDialog

class AnswerScoreFragment : Fragment() {
    private lateinit var viewModel: AnswerViewModel
    private lateinit var binding: FragmentAnswerScoreBinding
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        viewModel = ViewModelProvider(this).get(AnswerViewModel::class.java)
        binding =
            DataBindingUtil.inflate(inflater, R.layout.fragment_answer_score, container, false)
        binding.apply {
            lifecycleOwner = viewLifecycleOwner
            vw = this@AnswerScoreFragment
            my = MyApplication.Companion
        }
        viewModel.saveAnswer().observe(viewLifecycleOwner) {
            if (!it.success) {
                CircleDialog.Builder().setText(it.message).setNegative("确定", null)
                    .show(parentFragmentManager)
            }
        }
        return binding.root
    }

    fun gotoBack() = MyApplication.goto(
        R.id.action_answerScore_to_PartyHistoryAnswer,
        findNavController(),
        parentFragmentManager
    )

    fun gotoRanking() {
        //subject的话去排行榜
        if (MyApplication.subjectList.isNotEmpty() && MyApplication.subjectList.size >= 1 && MyApplication.subjectList[0].subjectTitle != "")
            MyApplication.goto(
                R.id.action_answerScore_to_Ranking,
                findNavController(),
                parentFragmentManager
            )
        //test的话无排行榜
        if (MyApplication.testList.isNotEmpty() && MyApplication.testList.size >= 1 && MyApplication.testList[0].testId != -1)
            CircleDialog.Builder().setText("每日答题、每周答题和专项答题无排行榜！").setTextColor(Color.BLUE)
                .setNegative("确定", null).show(parentFragmentManager)
    }
}